﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class CMS_Control_SetTopChoose : System.Web.UI.UserControl
{

    private string _StartStr;
    private string _SetId;
    private bool _IsChoose;
    private string _name = "";

    public string Name
    {
        set
        {
            _name = value;
        }
        get
        {
            return _name;
        }
    }

    public string StartStr
    {
        set
        {
            _StartStr = value;
        }
        get
        {
            return _StartStr;
        }
    }

    public bool IsChoose
    {
        set
        {
            _IsChoose = value;
        }
        get
        {
            return _IsChoose;
        }
    }

    public string SetId
    {
        set
        {
            hidarea1.Value = value;
        }
        get
        {
            return hidarea1.Value;
        }
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (hidarea1.Value.Trim() != "")
            {
                Usas.BLL.Cms.cmsArticle ar = new Usas.BLL.Cms.cmsArticle();
                Usas.BLL.Cms.cmsBorder bo = new Usas.BLL.Cms.cmsBorder();
                Usas.BLL.Cms.cmsType tp = new Usas.BLL.Cms.cmsType();

                try
                {
                    string table = hidarea1.Value.Substring(0, 3);
                    int id = Convert.ToInt32(hidarea1.Value.Substring(3));
                    choose.Value = "精华目录 >> ";
                    switch (table)
                    {
                        case "333":
                            choose.Value += ar.GetModel(id).ArtTopic;
                            break;
                        case "222":
                            choose.Value += bo.GetModel(id).BorderName;
                            break;
                        case "111":
                            choose.Value += tp.GetModel(id).TypeName;
                            break;
                    }
                }
                catch
                { }

            }

            if (StartStr != null && StartStr.Length > 0)
            {
                string[] sp = StartStr.Split('#');

                SetTopInit(sp[0], sp[1], sp[2], sp[3]);

                if (IsChoose == false)
                {
                    choose.Disabled = true;

                }
                else
                {

                    choose.Disabled = false;

                }

                if (Name.Trim() != "")
                {
                    choose.Value = "目录 >> " + Name;
                }

            }
            else
            {
                hidarea1.Value = "";
                choose.Disabled = true;
            }
        }
    }

    public void SetTopInit(string table, string id, string stable, string sid)
    {
        SetValue(table, id, stable, sid, new ArrayList(), new ArrayList());
    }

    private void SetValue(string table, string id, string stable, string spid, ArrayList arr, ArrayList arrname)
    {
        Usas.BLL.Cms.cmsArticle ar = new Usas.BLL.Cms.cmsArticle();
        Usas.BLL.Cms.cmsBorder bo = new Usas.BLL.Cms.cmsBorder();
        Usas.BLL.Cms.cmsType tp = new Usas.BLL.Cms.cmsType();
        Usas.Model.Cms.cmsArticle arm = new Usas.Model.Cms.cmsArticle();
        Usas.Model.Cms.cmsBorder bom = new Usas.Model.Cms.cmsBorder();
        Usas.Model.Cms.cmsType tpm = new Usas.Model.Cms.cmsType();

        switch (stable)
        {
            case "a":


                arm = ar.GetModel(Convert.ToInt32(spid));
                arr.Add("333" + arm.ArtId);
                arrname.Add(arm.ArtTopic);
                if (id == spid && table == stable)
                {
                    SetValue(table, id, "root", "-1", arr, arrname);
                }
                else
                {
                    if (arm.ParentArtId == 0)
                    {
                        SetValue(table, id, "b", arm.BorderId.ToString(), arr, arrname);
                    }
                    else
                    {
                        SetValue(table, id, "a", arm.ParentArtId.ToString(), arr, arrname);
                    }
                }
                break;
            case "b":
                bom = bo.GetModel(Convert.ToInt32(spid));
                arr.Add("222" + bom.BorderId);
                arrname.Add(bom.BorderName);
                if (id == spid && table == stable)
                {
                    SetValue(table, id, "root", "-1", arr, arrname);
                }
                else
                {
                    SetValue(table, id, "t", bom.TypeId.ToString(), arr, arrname);
                }
                break;
            case "t":
                tpm = tp.GetModel(Convert.ToInt32(spid));
                arr.Add("111" + tpm.TypeId);
                arrname.Add(tpm.TypeName);
                SetValue(table, id, "root", "-1", arr, arrname);
                break;
            case "root":
                arr.Add(spid);
                arrname.Add(stable);
                Show(arr, arrname);
                break;
        }
    }

    private void Show(ArrayList arrid, ArrayList arrname)
    {
        string tempId = "-1";
        string getJson = "";
        for (int i = arrid.Count - 1; i >= 0; i--)
        {
            if (arrid[i] == "-1")
            {
                getJson += " N[\"0_-1\"] = \"T:Hyperion\";";
                tempId = "-1";
            }
            else
            {
                getJson += "N[\"" + tempId + "_" + arrid[i].ToString() + "\"] = \"T:" + arrname[i].ToString().Trim() + ";C:L(" + arrid[i].ToString() + ")\";";
                tempId = arrid[i].ToString();
            }
        }

        this.Page.ClientScript.RegisterStartupScript(GetType(), "Msg", ";function SetValue(tree) {with (tree) {" + getJson + "}};Init()", true);
        //this.Page.ClientScript.RegisterStartupScript(GetType(), "Msg", ";window.onload=Init()", true);
    }


}
